{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "28cdd559",
   "metadata": {},
   "source": [
    "-----\n",
    "* 时间：2022-11-21/week14\n",
    "* 课程：python\n",
    "* 学习记录人：范昕妍\n",
    "-----"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9bc8b62",
   "metadata": {},
   "source": [
    "## 1.函数的定义"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3fbf42ac",
   "metadata": {},
   "source": [
    "# 本周目标\n",
    "> 1. 函数的定义\n",
    "> 2. 如何使用函数\n",
    "> 3. 函数的返回值\n",
    "> 4. 使用注释改进文档\n",
    "> 5. 传递任意数量的实参\n",
    "> 6. 函数与模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "ad5c1deb",
   "metadata": {},
   "outputs": [],
   "source": [
    "def greet_user(username):\n",
    "    \"\"\"对某人的问候语\"\"\"\n",
    "    print(\"hello \"+username+\"~\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "374cd646",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello 汤姆~\n"
     ]
    }
   ],
   "source": [
    "greet_user(\"汤姆\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0c37fb33",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "   \"\"\"y=2x的函数计算\"\"\"\n",
    "   print(2*x)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9e1dd2d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n"
     ]
    }
   ],
   "source": [
    "f(9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "b0508173",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实践 编程思维（计算思维）下，函数的应用\n",
    "def nfu_news_url(类别,page):\n",
    "    for page in range(page):\n",
    "        print(\"http://www.nfu.edu.cn/\" + 类别 + \"/index\" + str(page) + \".htm\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "bd753fd4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "http://www.nfu.edu.cn/tzgg/index0.htm\n",
      "http://www.nfu.edu.cn/tzgg/index1.htm\n",
      "http://www.nfu.edu.cn/tzgg/index2.htm\n",
      "http://www.nfu.edu.cn/tzgg/index3.htm\n",
      "http://www.nfu.edu.cn/tzgg/index4.htm\n",
      "http://www.nfu.edu.cn/tzgg/index5.htm\n",
      "http://www.nfu.edu.cn/tzgg/index6.htm\n",
      "http://www.nfu.edu.cn/tzgg/index7.htm\n",
      "http://www.nfu.edu.cn/tzgg/index8.htm\n",
      "http://www.nfu.edu.cn/tzgg/index9.htm\n",
      "http://www.nfu.edu.cn/tzgg/index10.htm\n",
      "http://www.nfu.edu.cn/tzgg/index11.htm\n",
      "http://www.nfu.edu.cn/tzgg/index12.htm\n",
      "http://www.nfu.edu.cn/tzgg/index13.htm\n",
      "http://www.nfu.edu.cn/tzgg/index14.htm\n"
     ]
    }
   ],
   "source": [
    "nfu_news_url(\"tzgg\",15)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4fb3923",
   "metadata": {},
   "source": [
    "## 2.如何使用函数(传递实参)\n",
    "\n",
    "> 1. positional argument(位置参数)\n",
    "> 2. keyword argument(关键字参数)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a669d6d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x,y):\n",
    "    print(x)\n",
    "    print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "f3c04136",
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "positional argument follows keyword argument (3059410298.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Input \u001b[1;32mIn [8]\u001b[1;36m\u001b[0m\n\u001b[1;33m    f(x=3,1)\u001b[0m\n\u001b[1;37m           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m positional argument follows keyword argument\n"
     ]
    }
   ],
   "source": [
    "f(x=3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f9e37e7c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "feb3cf3d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "f(x=3,y=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "43bb7587",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a hamster.\n",
      "Myhamster's name is Harry.\n",
      "\n",
      "I have a cat.\n",
      "Mycat's name is Python.\n",
      "\n",
      "I have a dog.\n",
      "Mydog's name is Jerry.\n"
     ]
    }
   ],
   "source": [
    "def describe_pet(pet_name,animal_type=\"dog\"):\n",
    "    \"\"\"显示宠物信息，如果您的宠物是dog，可以不选择填写animal_type\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My\" + animal_type + \"'s name is \" + pet_name.title() + \".\")\n",
    "    \n",
    "describe_pet('harry','hamster')\n",
    "describe_pet('python','cat')\n",
    "describe_pet('Jerry')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f264826d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "Mydog's name is Jerry.\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "print(describe_pet('Jerry'))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "17a4b593",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello|||||world|||||你好|||||世界\n",
      "hello world 你好 世界\n"
     ]
    }
   ],
   "source": [
    "print(\"hello\",\"world\",\"你好\",\"世界\",sep='|||||')\n",
    "print(\"hello\",\"world\",\"你好\",\"世界\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "25283c3e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function print in module builtins:\n",
      "\n",
      "print(...)\n",
      "    print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)\n",
      "    \n",
      "    Prints the values to a stream, or to sys.stdout by default.\n",
      "    Optional keyword arguments:\n",
      "    file:  a file-like object (stream); defaults to the current sys.stdout.\n",
      "    sep:   string inserted between values, default a space.\n",
      "    end:   string appended after the last value, default a newline.\n",
      "    flush: whether to forcibly flush the stream.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(print)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e2235d3d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on function describe_pet in module __main__:\n",
      "\n",
      "describe_pet(pet_name, animal_type='dog')\n",
      "    显示宠物信息，如果您的宠物是dog，可以不选择填写animal_type\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(describe_pet)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "897806c7",
   "metadata": {},
   "source": [
    "## 3.函数的返回值\n",
    "> 1. 几乎所有编程语言，函数是要求必须要 写 返回值！\n",
    "> 2. Python作为高级语言，可以 不写 返回值，但是一般情下，也是要写的，不写的话，默认返回 None\n",
    "> 3. 函数的返回值：return\n",
    ">> 1. return 可以返回过程运算的结果\n",
    ">> 2. return 可以返回过程自定义结果（由程序开发人员/设计人员定制的）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "89d36ed9",
   "metadata": {},
   "source": [
    "## 4.函数的优势：\n",
    "* 可重复使用\n",
    "* 具有强大的可扩展性\n",
    "* 具有可定制化性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "1a688348",
   "metadata": {},
   "outputs": [],
   "source": [
    "def f(x):\n",
    "    y = x+2\n",
    "    return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "67bf8698",
   "metadata": {},
   "outputs": [],
   "source": [
    "result = f(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "5f64efe9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2be3f15",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
